package com.shouhuobao.visit;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.VisitDoorBean;

/**
 * 上门地址的历史记录
 * Created by lidandan on 2017/12/14.
 */

public class VisitHistoryRecordActivity extends BaseActivity2 {
    private ListView mListView;
    private VisitHistoryRecordAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showLoadingDialog(null);
        AppHttp.getInstance().getAddressHistoryRecord();
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.visit_history_record);
        super.findViewById();
        getAppTitle().setCommonTitle("历史记录");
        mListView = (ListView) findViewById(R.id.listview);
        mAdapter = new VisitHistoryRecordAdapter(null, getContext());
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                VisitDoorBean item = (VisitDoorBean) parent.getItemAtPosition(position);
                if (null == item) {
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("item", item);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.get_address_history_record:
                cancelLoadingDialog();
                final AppResult<VisitDoorBean> result = JSON.parseList(msg.obj, VisitDoorBean.class);
                if (result.isSuccess()) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mAdapter.setItems(result.getResultList(), true);
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return false;
            default:
                return super.handleMessage(msg);
        }

    }
}
